package com.ngm.tea.tea.service.impl;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import com.ngm.tea.common.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.tea.dao.GoodsDao;
import com.ngm.tea.tea.entity.Goods;
import com.ngm.tea.tea.service.GoodsService;

/**
 * 商品大类服务层
 * @author zjw
 * @version 1.0
 * @date 2017-01-03 15:51:34
 *
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
	
	@Resource
	private GoodsDao goodsDao;

	public GoodsDao getGoodsDao() {
		return goodsDao;
	}

	@Override
	public void insert(Goods goods) {
		goodsDao.insert(goods);
	}

	@Override
	public void deleteById(Long id) {
		goodsDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			goodsDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(Goods goods) {
		goodsDao.delete(goods);
	}

	@Override
	public void update(Goods goods) {
		goodsDao.update(goods);
	}

	@Override
	public Goods selectOne(Long id) {
		return goodsDao.selectOne(id);
	}

	@Override
	public List<Goods> selectAll() {
		return goodsDao.selectAll();
	}

	@Override
	public List<Goods> selectList(Goods goods, PageInfo pageInfo) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(goods));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return goodsDao.selectList(map);
	}

	@Override
	public Long count(Goods goods) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(goods));
		return goodsDao.count(map);
	}
	
	public String makeStr(Goods goods) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(goods.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + goods.getId()+"%'");
			} else {
				sb.append(" AND id LIKE '%" + goods.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(goods.getName())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("name LIKE '%" + goods.getName()+"%'");
			} else {
				sb.append(" AND name LIKE '%" + goods.getName()+"%'");
			}
		}
		if(!StringUtils.isEmpty(goods.getType())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("type LIKE '%" + goods.getType()+"%'");
			} else {
				sb.append(" AND type LIKE '%" + goods.getType()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}
	
}